package com.hstcs.screen;

import com.badlogic.ashley.core.Engine;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.Table.Debug;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
import com.hstcs.assets.Assets;
import com.hstcs.debug.HDebug;
import com.hstcs.game.God;
import com.hstcs.game.XGame;
import com.hstcs.input.Hinput;
import com.hstcs.systmes.AnimationSystem;
import com.hstcs.systmes.BackgroundSystem;
import com.hstcs.systmes.BoundsSystem;
import com.hstcs.systmes.CameraSystem;
import com.hstcs.systmes.MaogouSystem;
import com.hstcs.systmes.MovementSystem;
import com.hstcs.systmes.RenderingSystem;
import com.hstcs.systmes.StateSystem;
import com.hstcs.systmes.TimeLineSystem;

public class GameScreen implements Screen {
	public static GameScreen instance =null;
	
	public static GameScreen getInstance()
	{
		if(instance == null){
			instance = new GameScreen();
		}
		return  instance;
	}
	
	private Stage stage = null;
	Engine engine;
	God  god;
	//ui----------------
	Touchpad touchpad;
	InputMultiplexer multiplexer = new InputMultiplexer();
	

	public void init(){
		engine = new Engine();
		god = new God(engine);
		stage = XGame.getInstance().stage;
		
		engine.addSystem(new BackgroundSystem());
		engine.addSystem(new RenderingSystem(stage.getBatch()));
		engine.getSystem(BackgroundSystem.class).setCamera(engine.getSystem(RenderingSystem.class).getCamera());
		engine.getSystem(RenderingSystem.class).getCamera().update();
		engine.addSystem(new MovementSystem());
		engine.addSystem(new MaogouSystem());
		engine.addSystem(new BoundsSystem());
		engine.addSystem(new StateSystem());
		engine.addSystem(new AnimationSystem());
		engine.addSystem(new CameraSystem());

						
		god.create();
		
	}
	
	public void createUI()
	{
		HDebug.getInstance().init(stage);
		
		multiplexer.addProcessor(stage);
		multiplexer.addProcessor(Hinput.getInstance());
		Gdx.input.setInputProcessor(multiplexer);	
	
	}
	
	
	@Override
	public void render(float delta) {
		// TODO Auto-generated method stub
		
		
		engine.update(delta);
		stage.act();
		stage.draw();
		HDebug.getInstance().update();
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		init();
		stage.clear();
		createUI();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	public Batch getBatch()
	{
		return stage.getBatch();
	}
	
}
